package org.game.module.event.listener;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;
import org.bean.ActivityEntryInfo;
import org.bean.ActivityInfo;
import org.cache.ActivityInfoCache;
import org.constant.ActivityConstant;
import org.constant.EventConditionKey;
import org.constant.EventConditionType;
import org.constant.EventType;
import org.constant.smallkey.ActivitySmallKey;
import org.game.db.bean.Player;
import org.game.db.cache.PlayerActivityCache;
import org.game.db.vo.BaseActivity;
import org.game.db.vo.ServerActivityProxy;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventListener;
import org.game.plugin.event.GameEventStack;
import org.game.plugin.event.GameEventType;

import com.sojoys.artifact.tools.ToolMap;
import com.xiaoleilu.hutool.date.DateUtil;

/**
 * 登陆事件监听器
 * 
 * @author SandKing
 *
 */
@GameEventType(EventType.LOGIN)
public class LoginEventListener implements GameEventListener {

	@Override
	public void onEvent(GameEvent event) {
		GameEventStack eventStack = event.peek();
		switch (eventStack.getConditionType()) {
		case EventConditionType.TOTAL_LOGIN_DAY: {
			boolean bool = true;
			
			Date lastLoginDate = ToolMap.getDate(EventConditionKey.LAST_LOGIN_DATE, event.getIn());
			Date nowLoginDate = ToolMap.getDate(EventConditionKey.NOW_LOGIN_DATE, event.getIn());
			bool &= checkPreEntryFinish(event.getPlayer(), eventStack);
			bool &= DateUtil.betweenDay(lastLoginDate, nowLoginDate, true) !=0 ;
			// 只有相差天数不等于0 才认为是新的一天登陆
			if (bool) {
				eventStack.changeProgress(1);
			}
		}
			break;
		case EventConditionType.CONT_LOGIN_DAY: {
			Date lastLoginDate = ToolMap.getDate(EventConditionKey.LAST_LOGIN_DATE, event.getIn());
			Date nowLoginDate = ToolMap.getDate(EventConditionKey.NOW_LOGIN_DATE, event.getIn());
			long day = DateUtil.betweenDay(lastLoginDate, nowLoginDate, true);
			// 只有相差天数等于1 才认为是连续登陆
			if (day == 1) {
				eventStack.changeProgress(1);
			} else {
				eventStack.setProgress(1);
			}
		}
			break;
		default:
			break;
		}
	}

	private boolean checkPreEntryFinish(Player player, GameEventStack eventStack) {
		boolean bool = true;
		// 需要活动条目XX完结
		int preEntryId = MapUtils.getIntValue(eventStack.getParams(), EventConditionKey.PRE_ENTRY_ID);
		if (preEntryId!=0) {
			ActivityEntryInfo entry = ActivityInfoCache.getEntry(preEntryId);
			ActivityInfo activityInfo = ActivityInfoCache.getById(entry.getActivityId());
			BaseActivity baseActivity = null;
			if (activityInfo!=null) {
				switch (activityInfo.getDataType()) {
				case ActivityConstant.ACTIVITY_DATA_TYPE_PLAYER:
					baseActivity = PlayerActivityCache.me().getByActivityIdPlayerId(activityInfo.getId(), player.getId());
					break;
				case ActivityConstant.ACTIVITY_DATA_TYPE_SERVER:
					baseActivity = new ServerActivityProxy(activityInfo);
					break;
				}
			}
			Map<Integer, Integer> entryMap = baseActivity.getEntry(player.getId()).computeIfAbsent(preEntryId, k -> new HashMap<>());
			int status = MapUtils.getIntValue(entryMap, ActivitySmallKey.STATUS ,0 );
			bool = (status == ActivityConstant.ENTRY_STATUS_RECEIVE);
		}
		return bool;
	}

	@Override
	public void initEventCondition(Player player, GameEventStack eventStack) {
		switch (eventStack.getConditionType()) {
		case EventConditionType.TOTAL_LOGIN_DAY:
		case EventConditionType.CONT_LOGIN_DAY: {
			Date lastLoginDate = player.getLastLoginTime();
			Date nowLoginDate = new Date();
			boolean bool = true;
			bool &= checkPreEntryFinish(player, eventStack);
			bool &= DateUtil.betweenDay(lastLoginDate, nowLoginDate, true) == 0;
			if (bool) {
				eventStack.changeProgress(1);
			}
		}
			break;
		}
	}

}
